﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WeatherHistory.Api.CommandsQueries.Commands.RegionTypes.Create;
using WeatherHistory.Api.CommandsQueries.Commands.RegionTypes.Delete;
using WeatherHistory.Api.CommandsQueries.Commands.RegionTypes.Update;
using WeatherHistory.Api.CommandsQueries.Queries.RegionTypes.GetOne;
using WeatherHistory.Api.CommandsQueries.Queries.RegionTypes.Search;
using WeatherHistory.Api.Filters;
using WeatherHistory.Shared.Validation;

namespace WeatherHistory.Api.Controllers;

/// <summary>
///     Контроллер для работы с типами регионов
/// </summary>
[Route("region/types")]
[ServiceFilter(typeof(AuthFilter))]
public class RegionTypesController : ApiControllerBase
{
    /// <summary>Создание типа региона</summary>
    [HttpPost]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(typeof(CreateRegionTypesCommandResponse), StatusCodes.Status201Created)]
    public async Task<ActionResult<CreateRegionTypesCommandResponse>> Create([FromBody] CreateRegionTypesCommandRequest request)
    {
        var result = await Mediator.Send(request);
        return Created($"/region/types/{result.Id}", result);
    }
    
    /// <summary>Поиск типов регионов</summary>
    [HttpGet("search")]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(typeof(SearchRegionTypesQueryResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<SearchRegionTypesQueryResponse>> Search([FromQuery] SearchRegionTypesQueryRequest request)
    {
        var result = await Mediator.Send(request);
        return Ok(result);
    }

    /// <summary>Получение информации о регионе</summary>
    [HttpGet("{typeId:long}")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(typeof(GetOneRegionTypesQueryResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<GetOneRegionTypesQueryResponse>> GetOne([FromRoute] [GreaterThan(0L)] long typeId)
    {
        var request = new GetOneRegionTypesQueryRequest();
        request.Id = typeId;

        var result = await Mediator.Send(request);
        return Ok(result);
    }

    /// <summary>Обновление типа региона</summary>
    [HttpPut("{typeId:long}")]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(UpdateRegionTypesCommandResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<UpdateRegionTypesCommandResponse>> Update([FromBody] UpdateRegionTypesCommandRequest request, [FromRoute] [GreaterThan(0L)] long typeId)
    {
        request.Id = typeId;
        var result = await Mediator.Send(request);
        return Ok(result);
    }

    /// <summary>Удаление типа региона</summary>
    [HttpDelete("{typeId:long}")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult> Delete([FromRoute] [GreaterThan(0L)] long typeId)
    {
        var request = new DeleteRegionTypesCommandsRequest();
        request.Id = typeId;

        await Mediator.Send(request);
        return Ok();
    }
}